package com.zfast.spring5.junit;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * @author zgn
 * @Description: DataSourceJunit
 * @date 2022/3/22 0022
 */
@SpringBootTest
public class DataSourceJunit {
    @Test
    public void egMin() {//e.g.
        System.out.println();
    }

    @Test
    public void egMax() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }
    @Autowired
    DataSourceProperties dataSourceProperties;

    @Autowired
    ApplicationContext applicationContext;

    @Resource(name = "myDataSource")
    private DataSource myDataSource;

    @Resource(name = "myDataSource2")
    private DataSource myDataSource2;

    @Test
    public void 默认数据源测试_egMin() {
        // 获取配置的数据源
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        // 查看配置数据源信息
        System.out.println(dataSource);
        System.out.println(dataSource.getClass().getName());
        System.out.println(dataSourceProperties);
        //执行SQL,输出查到的数据

        //region 1.默认数据源
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<?> resultList = jdbcTemplate.queryForList("select * from t_sys_user");//t_sys_role;t_sys_user
        for (int i = 0; i < resultList.size(); i++) {
            Object o =  resultList.get(i);
            System.out.println(o);
        }
        //endregion

        System.out.println("--------------------myDataSource--------------------");
        JdbcTemplate zgnTemplate = new JdbcTemplate(myDataSource);
        List<?> resultList2 = zgnTemplate.queryForList("select * from t_sys_user");//t_sys_role
        for (int i = 0; i < resultList2.size(); i++) {
            Object o =  resultList2.get(i);
            System.out.println(o);
        }

        System.out.println("--------------------myDataSource2--------------------");
        JdbcTemplate zgnTemplate2 = new JdbcTemplate(myDataSource2);
        List<?> resultList3 = zgnTemplate2.queryForList("select * from t_sys_user");//t_sys_role
        for (int i = 0; i < resultList3.size(); i++) {
            Object o =  resultList3.get(i);
            System.out.println(o);

        }
    }
}
